package com.nandi.deviceinstall.ui.activity

import android.content.Intent
import android.graphics.Bitmap
import android.graphics.BitmapFactory
import androidx.appcompat.app.AppCompatActivity
import android.os.Bundle
import android.view.View
import com.bumptech.glide.Glide
import com.bumptech.glide.load.engine.DiskCacheStrategy
import com.bumptech.glide.load.model.GlideUrl
import com.bumptech.glide.load.model.LazyHeaders
import com.bumptech.glide.request.RequestOptions
import com.nandi.deviceinstall.R
import com.nandi.deviceinstall.base.BaseMvpActivity
import com.nandi.deviceinstall.bean.BaseObjectBean
import com.nandi.deviceinstall.bean.LoginBean
import com.nandi.deviceinstall.bean.TapRecordInfoBean
import com.nandi.deviceinstall.common.Constant
import com.nandi.deviceinstall.contract.TapRecordInfoContract
import com.nandi.deviceinstall.presenter.TapRecordInfoPresenter
import com.nandi.deviceinstall.presenter.TapRecordPresenter
import com.nandi.deviceinstall.util.CustomToast
import com.nandi.deviceinstall.util.FileUtils
import com.nandi.deviceinstall.util.SharedTool
import kotlinx.android.synthetic.main.activity_tap_record_info.*
import kotlinx.android.synthetic.main.toolbar_back_include.*
import okhttp3.ResponseBody
import java.io.IOException

class TapRecordInfoActivity : BaseMvpActivity<TapRecordInfoPresenter>(), TapRecordInfoContract.View {
    private val strMonitorType = arrayOf("地表裂缝监测仪", "墙裂缝监测仪", "地面倾斜监测仪", "降雨量监测仪", "泥位监测仪")

    private var id: Int = 0
    private lateinit var info:TapRecordInfoBean

    override val layoutId: Int
        get() = R.layout.activity_tap_record_info


    override fun initView() {
        mPresenter = TapRecordInfoPresenter()
        mPresenter.attachView(this)
        tv_title_toolbar.text = "踩点详情"

        title_image.setImageResource(R.drawable.xiugai)
        right_tv.setText("修改")
        val extras = intent.extras
        id = extras.getInt("id")
        mPresenter.requestTapRecordInfo(SharedTool.get(context, Constant.TOKEN, "") as String, id)
        initListener()
    }

    private fun initListener() {
        iv_back_toolbar.setOnClickListener {
            finish()
        }

        title_image.setOnClickListener {

            val intent = Intent(context, EquipmentStepActivity::class.java)
            intent.putExtra("tapInfo",info)
            intent.putExtra("action","update")
            startActivity(intent)
        }

        downLoad.setOnClickListener {
            mPresenter.requestDownPic(SharedTool.get(context, Constant.TOKEN, "") as String, info.data!!.pointPic!!)
        }
    }

    override fun downPicSuccess(result: ResponseBody, fileName: String) {
        var bitmap: Bitmap? = null
        val bys: ByteArray
        var savePath = ""
        try {
            bys = result.bytes()
            bitmap = BitmapFactory.decodeByteArray(bys, 0, bys.size)
            try {
                FileUtils.saveImg(bitmap, Constant.IMG_PATH, fileName)
                showLongToast("下载成功,保存到${Constant.IMG_PATH + fileName}")
            } catch (e: IOException) {
                e.printStackTrace()
            }

        } catch (e: IOException) {
            e.printStackTrace()
        }
        bitmap?.recycle()
    }

    override fun handlerTapRecordResult(t: TapRecordInfoBean) {
        info = t
        district.text = t.data?.cityName
        township.text = t.data?.townName
        village.text = t.data?.village
        disasterName.text = t.data?.disasterName
        disasterNum.text = t.data?.disNo
        monitorType.text = strMonitorType[t.data?.monitorType!!.toInt()]
        serialNumber.text = t.data?.monitorNo!!.substring(t.data?.monitorNo!!.length - 2, t.data?.monitorNo!!.length)
        currlon.text = t.data?.monitorLng.toString()
        currlat.text = t.data?.monitorLat.toString()
        if (t.data!!.pointPic!!.isNotEmpty()){
            var imagePath = "${Constant.fileUrl}${t.data!!.pointPic}&type=6"
            val headers = LazyHeaders.Builder().addHeader("Authorization", SharedTool.get(context,
                    Constant.TOKEN, "") as String).build()
            val requestOptions = RequestOptions().error(R.mipmap.default_image)
                    .placeholder(R.mipmap.default_image).diskCacheStrategy(DiskCacheStrategy.NONE).skipMemoryCache(true)
            Glide.with(context)
                    //.asBitmap()

                    .load(GlideUrl(imagePath, headers))
                    .apply(requestOptions)
                    .into(iv)
        }
    }

    override fun showLoading() {

    }

    override fun hideLoading() {
    }

    override fun onError(msg: String, code: Int) {
        CustomToast.showToast(context,msg)
    }


}
